<?php
namespace app\pic\model;
use app\rbac\model\AdminM;
use think\Validate;
use think\Db;
use app\base\model\Base;
class PictureM extends Base
{

       protected function _V($scene,$data)
       {
           $rule = [
               'album_id' => 'require|number',
           ];
        $msg = [];
        $filed = [
            'album_id'=>'相册名称',
        ];
           $validate = new Validate($rule,$msg,$filed);
           $validate->scene('add', ['album_id']);
           $result = $validate->scene($scene)->check($data);
           if($result === false)
           {
               return $validate->getError();
           }
           else
           {
               return true;
           }

       }

    /**
     * 新增数据
     * @return mixed
     */

    public function add($data)
    {
        //验证数据
        $res = $this->_V('add',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $insertData = [];
        //判断用户
        $admin_id = session('admin_id');
        //数据


            /*********************处理图片相册-start******************/
            if (isset($_FILES['pic']))
            {
                $res = uploadMore('pic',array(
                    array(800, 800),
                    array(400, 400),
                    array(66, 66),
                ));
                if(isset($res['ok']))
                {
                    if($res['ok'] == 0)
                    {//出现错误
                        $this->code = -1;
                        $this->msg = $res['error'];
                        return false;
                    }
                    else
                    {//没有图片

                    }

                }
                else
                {//上传成功
                    foreach($res as $k=>$v)
                    {
                        $res2 = Db::table('pic_picture')->insertGetId([
                            'admin_id'=>$admin_id,
                            'album_id'=>$data['album_id'],
                            'pic' => toQiNiu($v['images'][0]),
                            'small_pic' => toQiNiu($v['images'][3]),
                            'mid_pic' => toQiNiu($v['images'][2]),
                            'big_pic' => toQiNiu($v['images'][1]),
                        ]);
                        if(!$res2)
                        {
                            $this->code = -1;
                            $this->msg = '报存图片到相册失败';
                            return false;
                        }

                    }

                }
            }
        /*********************处理图片相册-end******************/
        // 提交事务
        return true;
    }

    /**
     * 全部数据
     */
    public function lst($page = 15,$album_id)
    {
        $where = [];
        //其他条件
        //未删除的
        $where['p.is_usable'] = 1;
        $where['p.album_id'] = $album_id;

        $data = Db::table('pic_picture')
            ->alias('p')
            ->join('pic_album a','a.id = p.album_id','LEFT')
            ->field('p.id,p.album_id,p.small_pic,a.name as album_name')
            ->where($where)
            ->paginate($page);
        return $data;
    }

    /**
     * 修改数据
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        $res = $this->_V('edit',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $insertData = [];
        //判断用户
        $admin_id = session('admin_id');
        //数据
        $insertData['goods_name'] = $data['goods_name'];
        $insertData['goods_desc'] = $data['goods_desc'];
        $insertData['goods_sn'] = $data['goods_sn'];
        $insertData['ps_attr'] = $data['ps_attr'];
        $insertData['psfs_id'] = $data['psfs_id'];
        $insertData['brand_id'] = $data['brand_id'];
        $insertData['cat_id'] = $data['cat_id'];
        $insertData['type_id'] = $data['type_id'];
        $insertData['is_new'] = $data['is_new'];
        $insertData['is_hot'] = $data['is_hot'];
        $insertData['is_best'] = $data['is_best'];
        $insertData['is_floor'] = $data['is_floor'];
        $insertData['shop_id'] = $data['shop_id'];
        $insertData['is_discount'] = $data['is_discount'];
        $insertData['is_on_sale'] = $data['is_on_sale'];
        $insertData['sort_num'] = $data['sort_num'];
        //时间
        if(!empty($data['promote_start_date']))
        {
            $insertData['promote_start_date'] = strtotime($data['promote_start_date']);
        }
        if(!empty($data['promote_end_date']))
        {
            $insertData['promote_end_date'] = strtotime($data['promote_end_date']);
        }
        $insertData['updated_at'] = time();
        //主图

        //处理图片
        $ret = uploadOne('logo', array(
            array(800, 800),
            array(600, 600),
            array(300, 300),
            array(100, 100),
        ));
        if($ret['ok'] == 1)
        {
            $insertData['logo'] = toQiNiu($ret['images'][0]);   // 原图地址
            $insertData['mbig_logo'] = toQiNiu($ret['images'][1]);   // 第一个缩略图地址
            $insertData['big_logo'] = toQiNiu($ret['images'][2]);   // 第二个缩略图地址
            $insertData['mid_logo'] = toQiNiu($ret['images'][3]);   // 第三个缩略图地址
            $insertData['sm_logo'] = toQiNiu($ret['images'][4]);   // 第三个缩略图地址
        }
        else if($ret['ok'] == 2)
        {
        }
        else
        {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
        }
        $id = $data['id'];
        //启动事务
        Db::startTrans();
        //商品插入数据库
        $res = Db::table('shop_goods')->where('id',$id)->update($insertData);
        if (!$res) {
            $this->code = -1;
            $this->msg = '修改失败';
            Db::rollback();
            return false;
        }
        //属性

        /*********************处理商品属性-start******************/
        $gaid=$data['goods_attr_id'];
        $attr_value=$data['attr_value'];
//            dump($attr_value);die;
        $_i=0;
        foreach($attr_value as $k=>$v){
            foreach($v as $k1=>$v1){
                if($gaid[$_i] == ''){
                    $res = Db::table('shop_goods_attr')->insertGetId([
                        'attr_id'=>$k,
                        'attr_value'=>$v1,
                        'goods_id'=>$id,
                        'admin_id'=>$admin_id,
                    ]);
                }else {
                    $res = Db::table('shop_goods_attr')->where('id',$gaid[$_i])->setField('attr_value',$v1);
                }
                $_i++;
            }
        }
        /*********************处理商品属性-end******************/
        //图片

        /*********************处理图片相册-start******************/
        if (isset($_FILES['pic']))
        {
            $res = uploadMore('pic',array(
                array(450, 450),
                array(260, 260),
                array(66, 66),
            ));
            if(isset($res['ok']))
            {
                if($res['ok'] == 0)
                {//出现错误
                    $this->code = -1;
                    $this->msg = $res['error'];
                    Db::rollback();
                    return false;
                }
                else
                {//没有图片

                }

            }
            else
            {//上传成功
                foreach($res as $k=>$v)
                {
                    $res2 = Db::table('shop_goods_pic')->insertGetId([
                        'goods_id' => $id,
                        'pic' => toQiNiu($v['images'][0]),
                        'small_pic' => toQiNiu($v['images'][3]),
                        'mid_pic' => toQiNiu($v['images'][2]),
                        'big_pic' => toQiNiu($v['images'][1]),
                    ]);
                    if(!$res2)
                    {
                        $this->code = -1;
                        $this->msg = '报存图片到相册失败';
                        Db::rollback();
                        return false;
                    }

                }

            }
        }
        /*********************处理图片相册-end******************/
        // 提交事务
        Db::commit();
        return true;
    }

    /**
     * 删除数据
     * @param $id
     */
    public function del($id)
    {
        return Db::table('pic_picture')->where('id',$id)->update(['is_usable'=>0]);
    }

    /**
     * 删除数据
     * @param $id
     */
    public function review($id)
    {
        return Db::table('shop_goods')->where('id',$id)->update(['is_sh'=>1]);
    }





}
